package com.ruoyi.web.controller.news.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.news.query.NewsInfoQuery;
import com.ruoyi.system.domain.news.vo.NewsInfoVo;
import com.ruoyi.system.mapper.news.NewsInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/news/newsRest")
@Api(value = "newsRestApiController", tags  = "网站其他接口Api")
public class NewsRestApiController extends BaseController {

	@Resource
	private NewsInfoMapper newsInfoMapper;

	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "id", value = "id", dataType = "Long"),
			@ApiImplicitParam(paramType = "query", name = "tableName", value = "表名称", dataType = "String")
	})
	@ApiOperation(value = "获取单个文章信息", notes = "获取单个文章信息")
	@GetMapping("/getNewsInfoById")
	public R<NewsInfoVo> getById(@RequestParam(value = "id") Long id,
								 @RequestParam(value = "tableName") String tableName) {
		NewsInfoQuery newsInfoQuery = new NewsInfoQuery();
		newsInfoQuery.setId(id);
		newsInfoQuery.setTableName(tableName);
		newsInfoQuery.setIsEnable("1");
		NewsInfoVo newsInfoVo = newsInfoMapper.getNewsInfo(newsInfoQuery);
		if (newsInfoVo!=null) {
			return R.ok(newsInfoVo);
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "搜索文章信息列表")
	@PostMapping(value = "/searchNewsList")
	public R<PageInfo<NewsInfoVo>> list(@RequestBody NewsInfoQuery newsInfoQuery) {
		if (newsInfoQuery.getTitle()==null||newsInfoQuery.getTitle().equals("")) {
			return R.fail("请输入查询内容");
		}
        newsInfoQuery.setIsEnable("1");
		if (newsInfoQuery.getIfPage()) {
			PageHelper.startPage(newsInfoQuery.getPageNum(), newsInfoQuery.getPageSize());
		}
		List<NewsInfoVo> newsInfoList = newsInfoMapper.getNewsInfoList(newsInfoQuery);
		return R.ok(new PageInfo<>(newsInfoList));
	}
}
